﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace LogGenerator
{
    class Process_2_generator
    {
        string role1;
        string role2;
        string role3;
       
        List<string> role1Members;
        List<string> role2Members;
        List<string> role3Members;
        
        public Process_2_generator()
        {
            role1 = "Client_Role";
            role2 = "Customer_Service_Role";
            role3 = "Loan_Authorization_Role";
            
            role1Members = new List<string>();
            role1Members.Add("Originator_1_1");
            role1Members.Add("Originator_1_2");
            role1Members.Add("Originator_1_3");

            role2Members = new List<string>();
            role2Members.Add("Originator_2_1");
            role2Members.Add("Originator_2_2");
            role2Members.Add("Originator_2_3");
            
            role3Members = new List<string>();
            role3Members.Add("Originator_3_1");
            role3Members.Add("Originator_3_2");
            role3Members.Add("Originator_3_3");
        }

        private string GenerateTrace(string traceID,
            bool reviewHistoryActivityOutputOK,
            bool reviewCreditActivityOutputOK)
        {
            Random randnum = new Random();
            StringBuilder output = new StringBuilder();
            output.Append("<ProcessInstance id=\"");
            output.Append(traceID);
            output.Append("\">");

            output.Append("<AuditTrailEntry>");
            output.Append("<WorkflowModelElement>Request_loan</WorkflowModelElement>");
            output.Append("<Timestamp>");
            output.Append(DateTime.Now.ToString("o"));
            output.Append("</Timestamp>");
            output.Append("<Originator>");
            output.Append(role1Members[randnum.Next(1000) % 3]);
            output.Append("</Originator>");
            output.Append("<Role>");
            output.Append(role1);
            output.Append("</Role>");
            output.Append("<Output>request_document</Output>");
            output.Append("</AuditTrailEntry>"); 

            output.Append("<AuditTrailEntry>");
            output.Append("<WorkflowModelElement>Receive_the_request</WorkflowModelElement>");
            output.Append("<Timestamp>");
            output.Append(DateTime.Now.ToString("o"));
            output.Append("</Timestamp>");
            output.Append("<Originator>");
            output.Append(role2Members[randnum.Next(1000) % 3]);
            output.Append("</Originator>");
            output.Append("<Role>");
            output.Append(role2);
            output.Append("</Role>");
            output.Append("<Input Originator=\"Request_loan\">request_document</Input>");
            output.Append("<Output>client_ID</Output>");
            output.Append("</AuditTrailEntry>");

            output.Append("<AuditTrailEntry>");
            output.Append("<WorkflowModelElement>Review_client_history</WorkflowModelElement>");
            output.Append("<Timestamp>");
            output.Append(DateTime.Now.ToString("o"));
            output.Append("</Timestamp>");
            output.Append("<Originator>");
            output.Append(role3Members[randnum.Next(1000) % 3]);
            output.Append("</Originator>");
            output.Append("<Role>");
            output.Append(role3);
            output.Append("</Role>");
            output.Append("<Input Originator=\"Receive_the_request\">client_ID</Input>");
            if (reviewHistoryActivityOutputOK)
                output.Append("<Output>history_ok</Output>");
            else
                output.Append("<Output>history_not_ok</Output>");
            output.Append("</AuditTrailEntry>");

            output.Append("<AuditTrailEntry>");
            output.Append("<WorkflowModelElement>Review_client_credit_info</WorkflowModelElement>");
            output.Append("<Timestamp>");
            output.Append(DateTime.Now.ToString("o"));
            output.Append("</Timestamp>");
            output.Append("<Originator>");
            output.Append(role3Members[randnum.Next(1000) % 3]);
            output.Append("</Originator>");
            output.Append("<Role>");
            output.Append(role3);
            output.Append("</Role>");
            output.Append("<Input Originator=\"Receive_the_request\">client_ID</Input>");
            if (reviewCreditActivityOutputOK)
                output.Append("<Output>credit_ok</Output>");
            else
                output.Append("<Output>credit_not_ok</Output>");
            output.Append("</AuditTrailEntry>");

            if (reviewHistoryActivityOutputOK && reviewCreditActivityOutputOK)
            {
                output.Append("<AuditTrailEntry>");
                output.Append("<WorkflowModelElement>Loan_request_approved</WorkflowModelElement>");
                output.Append("<Timestamp>");
                output.Append(DateTime.Now.ToString("o"));
                output.Append("</Timestamp>");
                output.Append("<Originator>");
                output.Append(role3Members[randnum.Next(1000) % 3]);
                output.Append("</Originator>");
                output.Append("<Role>");
                output.Append(role3);
                output.Append("</Role>");
                output.Append("<Input Originator=\"Review_client_history\">history_ok</Input>");
                output.Append("<Input Originator=\"Review_client_credit_info\">credit_ok</Input>");
                output.Append("<Output>loan_approved</Output>");
                output.Append("</AuditTrailEntry>");

                output.Append("<AuditTrailEntry>");
                output.Append("<WorkflowModelElement>Send_resolution</WorkflowModelElement>");
                output.Append("<Timestamp>");
                output.Append(DateTime.Now.ToString("o"));
                output.Append("</Timestamp>");
                output.Append("<Originator>");
                output.Append(role2Members[randnum.Next(1000) % 3]);
                output.Append("</Originator>");
                output.Append("<Role>");
                output.Append(role2);
                output.Append("</Role>");
                output.Append("<Input Originator=\"Loan_request_approved\">loan_approved</Input>");
                output.Append("<Output>loan_approved</Output>");
                output.Append("</AuditTrailEntry>");

                output.Append("<AuditTrailEntry>");
                output.Append("<WorkflowModelElement>Give_loan_documents</WorkflowModelElement>");
                output.Append("<Timestamp>");
                output.Append(DateTime.Now.ToString("o"));
                output.Append("</Timestamp>");
                output.Append("<Originator>");
                output.Append(role2Members[randnum.Next(1000) % 3]);
                output.Append("</Originator>");
                output.Append("<Role>");
                output.Append(role2);
                output.Append("</Role>");
                output.Append("<Input Originator=\"Send_resolution\">loan_approved</Input>");
                output.Append("<Output>loan_documents</Output>");
                output.Append("</AuditTrailEntry>");

                output.Append("<AuditTrailEntry>");
                output.Append("<WorkflowModelElement>Register_loan_documents</WorkflowModelElement>");
                output.Append("<Timestamp>");
                output.Append(DateTime.Now.ToString("o"));
                output.Append("</Timestamp>");
                output.Append("<Originator>");
                output.Append(role2Members[randnum.Next(1000) % 3]);
                output.Append("</Originator>");
                output.Append("<Role>");
                output.Append(role2);
                output.Append("</Role>");
                output.Append("<Input Originator=\"Give_loan_documents\">loan_documents</Input>");
                output.Append("<Output>loan_documents</Output>");
                output.Append("</AuditTrailEntry>");

                output.Append("<AuditTrailEntry>");
                output.Append("<WorkflowModelElement>Give_loan_money</WorkflowModelElement>");
                output.Append("<Timestamp>");
                output.Append(DateTime.Now.ToString("o"));
                output.Append("</Timestamp>");
                output.Append("<Originator>");
                output.Append(role2Members[randnum.Next(1000) % 3]);
                output.Append("</Originator>");
                output.Append("<Role>");
                output.Append(role2);
                output.Append("</Role>");
                output.Append("<Input Originator=\"Register_loan_documents\">loan_documents</Input>");
                output.Append("<Output>money</Output>");
                output.Append("</AuditTrailEntry>");

            }
            else
            {
                output.Append("<AuditTrailEntry>");
                output.Append("<WorkflowModelElement>Loan_request_rejected</WorkflowModelElement>");
                output.Append("<Timestamp>");
                output.Append(DateTime.Now.ToString("o"));
                output.Append("</Timestamp>");
                output.Append("<Originator>");
                output.Append(role3Members[randnum.Next(1000) % 3]);
                output.Append("</Originator>");
                output.Append("<Role>");
                output.Append(role3);
                output.Append("</Role>");
                if (reviewHistoryActivityOutputOK)
                    output.Append("<Input Originator=\"Review_client_history\">history_ok</Input>");
                else
                    output.Append("<Input Originator=\"Review_client_history\">history_not_ok</Input>");
                if (reviewCreditActivityOutputOK)
                    output.Append("<Input Originator=\"Review_client_credit_info\">credit_ok</Input>");
                else
                    output.Append("<Input Originator=\"Review_client_credit_info\">credit_not_ok</Input>");
                output.Append("<Output>loan_rejected</Output>");
                output.Append("</AuditTrailEntry>");

                output.Append("<AuditTrailEntry>");
                output.Append("<WorkflowModelElement>Send_resolution</WorkflowModelElement>");
                output.Append("<Timestamp>");
                output.Append(DateTime.Now.ToString("o"));
                output.Append("</Timestamp>");
                output.Append("<Originator>");
                output.Append(role2Members[randnum.Next(1000) % 3]);
                output.Append("</Originator>");
                output.Append("<Role>");
                output.Append(role2);
                output.Append("</Role>");
                output.Append("<Input Originator=\"Loan_request_rejected\">loan_rejected</Input>");
                output.Append("<Output>loan_rejected</Output>");
                output.Append("</AuditTrailEntry>");
            }
            output.Append("</ProcessInstance>");
            return output.ToString();
        }

        private string GenerateProcess(string processID, int numOfTraces)
        {
            StringBuilder output = new StringBuilder();
            output.Append("<Process id=\"");
            output.Append(processID);
            output.Append("\">");
            Random randnum = new Random();
            bool condition1 = false;
            bool condition2 = false;
            for (int idx = 0; idx < numOfTraces; idx++)
            {
                condition1 = randnum.Next(0, 2) != 0 ? true : false;
                condition2 = randnum.Next(0, 2) != 0 ? true : false;
                output.Append(GenerateTrace("instance_" + idx, condition1,condition2));
            }
            output.Append("</Process>");
            return output.ToString();
        }

        public void GenerateLog(string filename, int numOfTraces)
        {
            StringBuilder output = new StringBuilder();
            output.Append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
            output.Append("<WorkflowLog xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"  xmlns=\"XSDMXML\" xsi:schemaLocation=\"XSDMXML NewLog.xsd\">");
            output.Append("<Source program=\"Process_2_generator\"/>");
            output.Append(GenerateProcess("Process_2", 15));
            output.Append("</WorkflowLog>");
            StreamWriter swriter = new StreamWriter(filename + ".xml");
            swriter.Write(output.ToString());
            swriter.Close();
        }
    }
}
